####################################
#This is the replication file for:
# Burnworth, Justin. Nd. "The Supreme Court's Increasing Use of Legal Scholarship." Journal of Law and Courts
####################################

###################
#load packages
library(readxl)
library(ggplot2)
library(scales)
library(dplyr)
library(ggthemes)
library(tidyr)
library(tidyverse)
library(stringr)
library(purrr)
###################

###################
### Load in Data
#################

data <- read_excel("Roberts Court Citing to Law Reviews.xlsx")

#################
# aggregate at opinion level
#################

data <- data %>% 
  group_by(caseId, justice) %>%
  summarise(citation_count = n())

#################
#Add in Citation Count for federalist society professors 
#################

fed_soc_data <- Roberts_Court_Citing_to_Law_Reviews  %>%
  filter(FedSoc == 1) %>%
  group_by(caseId, justice) %>%
  summarise(FedSoc_count = n())

#################
#Load in SCDB justice centered data
#################
SCDB <- read_csv("SCDB_2024_01_justiceCentered_Citation.csv")

#################
#only keep Justices if they wrote an opinion in a case, "opinion == 2"
#################

Justice <- SCDB %>%
  mutate(dateDecision = as.Date(dateDecision, format = "%m/%d/%Y")) %>%
  filter(opinion == 2,
         dateDecision > as.Date("2005-09-30"))

#################################
#Combine Citation Data(Data), SCDB(Justice), and FedSoc Citation Data(fed_soc_data)
#################################

LawReviews <- Justice %>%
  left_join(data, by = c("caseId", "justice")) %>%
  left_join(fed_soc_data, by = c("caseId", "justice")) %>%
  mutate(
    citation_count = coalesce(citation_count, 0),
    FedSoc_count   = coalesce(FedSoc_count, 0)
  )

#################################
#Figure 1: Roberts Court Citation to Legal Scholarship Over Time
################################

TotalCitationsPerYear <- Roberts_Court_Citing_to_Law_Reviews %>%
  group_by(Year) %>%
  summarise(count=n())

#plot 

Figure1 <-ggplot(TotalCitationsPerYear, aes(Year, count )) + 
  geom_point(size = 4)+
  theme_clean()+
  labs(title = "Supreme Court Citations to Legal Scholarship",
       x = "Term",
       y = "Number of Citations")+
  theme(legend.title=element_blank())+
  theme(legend.text= element_text(face = "bold", size = 14))+
  scale_x_continuous(breaks = seq(2006, 2024, 4))+
  theme(plot.title = element_text(hjust = 0.5))+
  coord_cartesian(ylim = c(0, 250))+ 
  geom_smooth(method = "gam", color = "darkgrey", fill = "lightgrey")+
  theme_bw()+
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x = element_text(face = "bold", size = 18, angle = 45, hjust = 1),
    axis.text.y = element_text(face = "bold", size = 18),
    legend.title = element_blank(),
    legend.text = element_text(face = "bold", size = 18)
  )

# Save the plot as a TIFF file
ggsave("Figure 1.tiff", 
       plot = Figure1, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 


################################
#Figure 2: The Proportion of Opinions That Cite Legal Scholarship Per Term
###############################

#Create binary variable if an opinion cited a law review article
LawReviews <- LawReviews %>%
  mutate(cites_law_review = ifelse(citation_count > 0, 1, 0))

#determine percent of opinions that cite law review articles
law_review_year <- LawReviews %>%
  group_by(term) %>%  
  summarize(
    total_opinions = n(),
    opinions_citing = sum(cites_law_review, na.rm = TRUE),
    percent_citing = opinions_citing / total_opinions
  ) %>%
  ungroup()

#convert to percentage
law_review_year <- law_review_year %>%
  mutate(percent_citing = percent_citing * 100)

#plot
Figure2 <- ggplot(law_review_year, aes(x = term, y = percent_citing)) +
  geom_line() +
  geom_point(size = 4) +
  scale_x_continuous(breaks = seq(min(law_review_year$term),
                                  max(law_review_year$term),
                                  by = 1)) +
  scale_y_continuous(labels = label_percent(scale = 1)) +
  labs(
    title = "Proportion of Opinions that Cited Legal Scholarship by Term",
    x = "Term",
    y = "Proportion"
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x = element_text(face = "bold", size = 18, angle = 45, hjust = 1),
    axis.text.y = element_text(face = "bold", size = 18),
    legend.title = element_blank(),
    legend.text = element_text(face = "bold", size = 18)
  )

# Save the plot as a TIFF file
ggsave("Figure 2.tiff", 
       plot = Figure2, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 


###############################
#Figure 3: Proportion of Opinion Types That Justices Cite Legal Scholarship Per Term
###############################


#create opinion_type varible
LawReviews <- LawReviews %>%
  mutate(opinion_type = case_when(
    vote == 1 ~ "Majority",
    vote == 2 ~ "Dissent",
    vote %in% c(3,4) ~ "Concurrence",
    TRUE ~ NA_character_
  ))

#summarize by term and opinion type
lr_by_term_type <- LawReviews %>%
  filter(!is.na(opinion_type)) %>%
  group_by(term, opinion_type) %>%
  summarize(
    n_opinions = n(),
    prop_citing = mean(cites_law_review, na.rm = TRUE),
    .groups = "drop"
  )

#create percent_citing variable 
lr_by_term_type <- lr_by_term_type %>%
  mutate(percent_citing = prop_citing * 100)


#plot
Figure3<- ggplot(lr_by_term_type,
       aes(x = term,
           y = prop_citing,
           shape   = opinion_type,
           colour =  opinion_type)) +
  geom_line(linewidth = 1.2) +
  geom_point(size = 4) +
  scale_color_grey(start = 0.2, end = 0.7) +
  scale_y_continuous(labels = label_percent(), limits = c(0,1)) +
  labs(
    title = "Proportion of Opinion Types Citing Legal Scholarship",
    x = "Term",
    y = "Proportion ",
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x = element_text(face = "bold", size = 18),
    axis.text.y = element_text(face = "bold", size = 18),
    legend.title = element_blank(),
    legend.text = element_text(face = "bold", size = 18)
  )

# Save the plot as a TIFF file
ggsave("Figure 3.tiff", 
       plot = Figure3, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 


################################
#Figure 4: The Proportion of Citations Based on Issue Area
################################

#Create binary variable if an opinion cited a law review article
LawReviews <- LawReviews %>%
  mutate(cites_law_review = ifelse(citation_count > 0, 1, 0))


#group by SCDB issueArea, create cases_citing_lr and proporiton_of_citing_lr
IssueAreaProp <- LawReviews %>%
  filter(!is.na(issueArea)) %>%
  group_by(issueArea) %>%
  summarise(
    total_cases = n(),
    cases_citing_lr = sum(cites_law_review == 1, na.rm = TRUE),
    prop_citing_lr = cases_citing_lr / total_cases,
    .groups = "drop"
  )


#remove any NAs
IssueAreaProp <- na.omit(IssueAreaProp)


#Recode issueArea from numbers in SCDB to their appropriate names
df <- IssueAreaProp %>%
  mutate(IssueText = recode(issueArea,
                            `1` = "Criminal Procedure",
                            `2` = "Civil Rights",
                            `3` = "First Amendment",
                            `4` = "Due Process",
                            `5` = "Privacy",
                            `6` = "Attorneys",
                            `7` = "Unions",
                            `8` = "Economic Activity",
                            `9` = "Judicial Power",
                            `10` = "Federalism",
                            `11` = "Interstate Relations",
                            `12` = "Federal Taxation",
                            `13` = "Miscellaneous",
                            `14` = "Private Action"
  ))


#plot
Figure4 <- ggplot(df,
       aes(x = reorder(IssueText, prop_citing_lr),
           y = prop_citing_lr)) +
  geom_col() +
  coord_flip() +
  scale_y_continuous(
    labels = scales::percent_format(accuracy = 1),
    expand = expansion(mult = c(0, 0.15))
  )  +
  labs(
    title = "Proportion of Cases Citing Legal Scholarship by Issue Area",
    y = "Proportion of Cases Citing Law Reviews",
    x = ""
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x  = element_text(face = "bold", size = 14),
    axis.text.y  = element_text(face = "bold", size = 18)
  )

# Save the plot as a TIFF file
ggsave("Figure 4.tiff", 
       plot = Figure4, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 

################################
#Figure 5: The Cases with the Most Citations to Legal Scholarship. 
################################

#sort cases by the top 10 that cited the most legal scholarship
Cases <- Roberts_Court_Citing_to_Law_Reviews %>%
  count(caseName) %>%
  filter(n > 22)

#arrange cases by number of citations
Cases <- Cases %>%
  arrange(n)

#plot
Figure5 <- ggplot(data = Cases , aes(x = caseName, y = n)) +
  geom_point(aes(x = factor(caseName, level = caseName), size =4)) +
  theme(axis.text.x = element_text(vjust=0.5, face = "bold"), axis.title.y = element_text(face = "bold"), plot.title = element_text(face = "bold"), axis.title.x = element_text(face = "bold"), axis.text.y = element_text(face = "bold")) +
  labs(title = "Cases with the Most Citatons to Legal Scholarship", element_text(face = "bold"), y = "Total Number of Citations",element_text(face = "bold"), x = "", element_text(face = "bold")) +
  coord_flip()+
  theme_bw() +
  theme(legend.position="none")+
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x = element_text(face = "bold", size = 18),
    axis.text.y = element_text(face = "bold", size = 12),
    legend.title = element_blank()
  )

# Save the plot as a TIFF file
ggsave("Figure 5.tiff", 
       plot = Figure5, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 

################################
#Figure 6: The Percentage of Cases Citing Legal Scholarship by Justice
################################


justice_lr_percent <- LawReviews %>%
  group_by(justiceName, caseId) %>%                 # collapse to case level
  summarise(cites_lr = max(cites_law_review), .groups = "drop") %>%  # 1 if any citation in the case
  group_by(justiceName) %>%
  summarise(
    total_cases = n(),
    law_review_cases = sum(cites_lr),
    pct_cases_citing_lr = 100 * law_review_cases / total_cases,
    .groups = "drop"
  )

#Remove NAs
justice_lr_percent <- na.omit(justice_lr_percent)


# Ensure justice_lr_percent exists from previous step
justice_lr_percent <- justice_lr_percent %>%
  arrange(pct_cases_citing_lr) %>%                # sort for plotting
  mutate(justiceName = factor(justiceName, levels = justiceName))  # preserve order

#Recode Justice names for clairity on figure
justice_lr_percent <- justice_lr_percent %>%
  mutate(justiceName = recode(justiceName,
                              `NMGorsuch` = "Gorsuch",
                              `CThomas` = "Thomas",
                              `BMKavanaugh` = "Kavanaugh",
                              `KBJackson` = "Jackson",
                              `ACBarrett` = "Barrett",
                              `EKagan` = "Kagan",
                              `RBGinsburg` = "Ginsburg",
                              `SSotomayor` = "Sotomayor",
                              `JGRoberts` = "Roberts",
                              `SGBreyer` = "Breyer",
                              `AMKennedy` = "Kennedy",
                              `SAAlito` = "Alito",
                              `JPStevens` = "Stevens",
                              `AScalia` = "Scalia",
                              `DHSouter` = "Souter",
                              `SDOConnor` = "OConnor"
  ))


#plot
Figure6 <- ggplot(justice_lr_percent, aes(x = justiceName, y = pct_cases_citing_lr)) +
  geom_col() +
  coord_flip() +   
  scale_y_continuous(labels = label_percent(scale = 1)) +
  labs(
    x = "Justice",
    y = "Percent of Cases Citing Law Reviews",
    title = "Justice-Level Law Review Citation Rates"
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x  = element_text(face = "bold", size = 14),
    axis.text.y  = element_text(face = "bold", size = 18),
    legend.text  = element_text(face = "bold", size = 16)
  )

# Save the plot as a TIFF file
ggsave("Figure 6.tiff", 
       plot = Figure6, 
       units = "in", 
       width = 18, 
       height = 8, 
       dpi = 300) 

################################
#Figure 7: The Proportion of Citations Based on Law Review 
################################
LawReview <- Roberts_Court_Citing_to_Law_Reviews %>%
  filter(!is.na(Law_Review)) %>%
  count(Law_Review, name = "n") %>%
  mutate(prop = n / sum(n))

LawReview_top <- LawReview %>%
  slice_max(prop, n = 10)

Figure7 <- ggplot(data = LawReview_top, aes(x = reorder(Law_Review, prop), y = prop)) +
  geom_col() +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
  labs(
    title = "Proportion of Citations by Law Review",
    y = "Proportion of Citations",
    x = ""
  ) +
  theme_bw() +
  coord_flip()+
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x  = element_text(face = "bold", size = 18, angle = 0, hjust = 1),
    axis.text.y  = element_text(face = "bold", size = 14),
    legend.title = element_blank(),
    legend.text  = element_text(face = "bold", size = 18)
  )

# Save the plot as a TIFF file
ggsave("Figure 7.tiff", 
       plot = Figure7, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 

################################
#Figure 8: The Distribution of Citations to Legal Scholars
################################

#count the total number of professors
Professors <- Roberts_Court_Citing_to_Law_Reviews %>%
  count(Prof)

#count the number of professors listed as second author
Second <- Roberts_Court_Citing_to_Law_Reviews %>%
  group_by(Second_Author) %>%
  summarise(count=n())

#count the number of professors listed as third author
Third  <- Roberts_Court_Citing_to_Law_Reviews %>%
  group_by(Third_Autor) %>%
  summarise(count=n())

#Recode author names
Second <- Second %>% rename(Prof = Second_Author)
Third <- Third %>% rename(Prof = Third_Autor)

#combine all types of authorship
combined <- reduce(list(Professors, Second, Third), full_join, by = "Prof")


combined <- combined %>%
  mutate(total_count = rowSums(across(c(n, count.x, count.y)), na.rm = TRUE))

#get total count of citations for each professor(whether they were first, second, or third author)
combined$total_count <- as.numeric(combined$total_count)


#rename variable
professors <- combined   


professor <- na.omit(professors)

#get descriptive statsitcs for professor citation counts
sd(professors$total_count)
mean(professors$total_count)
median(professors$total_count)
quantile(professors$total_count, probs = 0.75)


#plot distribution of citations per professor 
Figure8 <- ggplot(professors, aes(x = total_count)) +
  geom_histogram(binwidth = 1, fill = "black", color = "white") +
  coord_cartesian(xlim = c(0, 50)) +
  labs(
    title = "Distribution of Citations Per Professor",
    x = "Number of Citations",
    y = "Number of Professors"
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x = element_text(face = "bold", size = 18),
    axis.text.y = element_text(face = "bold", size = 18),
    legend.title = element_blank(),
    legend.text = element_text(face = "bold", size = 18)
  )

# Save the plot as a TIFF file
ggsave("Figure 8.tiff", 
       plot = Figure8, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 


################################
#Figure 9: The Twenty Most-Cited Law Professors by the Roberts Court
################################

#Create counts of Professors 

ProfCounts <- Roberts_Court_Citing_to_Law_Reviews %>%
  filter(!is.na(Prof)) %>%
  group_by(Prof, FedSoc) %>%
  summarise(n = n(), .groups = "drop")

#create variable to filter by top 20 in total citation counts by the Court
top_n <- 20   

#order by top 20 
ProfTop <- ProfCounts %>%
  slice_max(order_by = .data$n, n = top_n)


#Attach Federalist Society affilaition to profs
ProfTop <- ProfTop %>%
  mutate(FedSocLabel = ifelse(FedSoc == 1, "FedSoc", "Non-FedSoc"))

#plot
Figure9 <- ggplot(ProfTop,
       aes(x = reorder(Prof, n),
           y = n,
           fill = factor(FedSoc))) +
  geom_col() +
  coord_flip() +
  labs(
    title = paste0("Top ", top_n, " Most-Cited Professors"),
    y = "Number of Citations",
    x = "",
    fill = "FedSoc"
  ) +
  scale_fill_manual(
    values = c("0" = "gray70", "1" = "black"),
    labels = c("Non-FedSoc", "FedSoc")
  ) +
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 24, hjust = 0.5),
    axis.title.x = element_text(face = "bold", size = 20),
    axis.title.y = element_text(face = "bold", size = 20),
    axis.text.x  = element_text(face = "bold", size = 14),
    axis.text.y  = element_text(face = "bold", size = 18),
    legend.text  = element_text(face = "bold", size = 16)
  )

# Save the plot as a TIFF file
ggsave("Figure 9.tiff", 
       plot = Figure9, 
       units = "in", 
       width = 15, 
       height = 8, 
       dpi = 300) 




